/*
 * FileName: validateId.js
 * CreateAt: 2017.07.07
 * Author: awoo
 * E-mail: awoooo@me.com
 */

function validateIdCode (id) {
  if (!/^\d{17}[0-9xX]{1}$/g.test(id)) return false

  let calcCode = '79x584216379x5842'
  let sum = 0
  for (let i = 0; i < calcCode.length; i++) {
    let auth = (i === 2 || i === 12) ? 10 : calcCode[i]
    sum += id[i] * auth
  }

  let checkCode = '10x98765432'
  return checkCode[sum % 11] === id[17].toLowerCase()
}
